package com.alp.scan.common;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.alp.scan.R;
import com.alp.scan.activity.CaptureActivity;


/**
 * Created by xuHuanLin on 2019/11/19.
 *
 * @author xuHuanLin
 * @Description
 */

public class PermissionSetting {

    public static final int CAMERA_REQUEST_CODE = 2;
    private static AlertDialog dialog;


    /**
     * 手机调用摄像头扫码
     *
     * @param fragment
     * @param request  RequestCode
     */
    public static void ShowScanWindows(EditText editText, android.app.Fragment fragment, int request) {
        EditTextCommon.setEditFocus(editText);
        ShowScanWindows(fragment, request);
    }

    /**
     * 手机调用摄像头扫码
     * @param fragment
     * @param request
     */
    public static void ShowScanWindows(android.app.Fragment fragment, int request) {
        if (EditTextCommon.booleanisCameraUseable()) {
            Intent intent = new Intent(fragment.getActivity(), CaptureActivity.class);
            fragment.startActivityForResult(intent, request);
        } else {
            if (EditTextCommon.booleanisCameraUseable()) {
                return;
            }
            showDialog(fragment.getActivity());
        }
    }

    /**
     * 手机调用摄像头扫码
     *
     * @param fragment
     * @param request  RequestCode
     */
    public static void ShowScanWindows(EditText editText, Fragment fragment, int request) {
        EditTextCommon.setEditFocus(editText);
        ShowScanWindows(fragment, request);
    }

    /**
     * 手机调用摄像头扫码
     * @param fragment
     * @param request
     */
    public static void ShowScanWindows(Fragment fragment, int request) {
        if (EditTextCommon.booleanisCameraUseable()) {
            Intent intent = new Intent(fragment.getActivity(), CaptureActivity.class);
            fragment.startActivityForResult(intent, request);
        } else {
            if (EditTextCommon.booleanisCameraUseable()) {
                return;
            }
            showDialog(fragment.getActivity());
        }
    }

    /**
     * 手机调用摄像头扫码
     * @param context
     * @param request  RequestCode
     */
    public static void ShowScanWindows(EditText editText, final Context context, int request) {
        EditTextCommon.setEditFocus(editText);
        ShowScanWindows(context, request);
    }

    /**
     * 手机调用摄像头扫码
     *
     * @param context
     * @param request RequestCode
     */
    public static void ShowScanWindows(final Context context, int request) {
        /*if (CommonUtil.isCameraCanUse()) {
            Intent intent = new Intent(getActivity(), CaptureActivity.class);
            startActivityForResult(intent, RequestCode.REQ_CODE);
        }*/
        if (EditTextCommon.booleanisCameraUseable()) {
            Intent intent = new Intent(context, CaptureActivity.class);
            ((Activity) context).startActivityForResult(intent, request);
        } else {
            showDialog(context);
        }
    }

    /**
     * 调用摄像头
     * @param activity
     */
    public static void requestCamera(Activity activity) {
        //有则不调用
        if (EditTextCommon.booleanisCameraUseable()) {
            return;
        }
        //获取相机拍摄读写权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
        }
    }

    /**
     * 异常弹窗
     * @param context
     */
    public static void showDialog(final Context context) {
        View view = View.inflate(context, R.layout.dlg_camera_setting, null);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        Button btnSetting = view.findViewById(R.id.btn_setting);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btnSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
                ((Activity) context).startActivity(intent);
                dialog.dismiss();
            }
        });
        dialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        Window window = dialog.getWindow();
        //这一句消除白块
        window.setBackgroundDrawable(new BitmapDrawable());

        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();


    }
}
